<style type="text/css">
    .time_add {
        margin-left: 15px;
        cursor: pointer;
        color: #0000ff;
    }

    .time_del {
        margin-left: 15px;
        cursor: pointer;
        color: #ff0000;
    }
</style>
<h2 class="contentTitle">添加推广排期</h2>
<form action="<?php echo SITE_URL . 'index.php?c='.$controller.'&a=ajaxAdd';?>" method="post" class="pageForm required-validate" onsubmit="return validateCallback(this, dialogAjaxDone)">
<div class="pageContent">
	<div layouth="97" class="pageFormContent nowrap _time_content" style="height: 376px; overflow: auto;">
        <dl>
            <dt>推广名称：</dt>
            <dd style="width: 400px" title="<?php echo $info['title']; ?>">
                <?php echo cnSubStr($info['title'],8,'...'); ?>
                <span style="margin-left: 15px" class="inputInfo">最多为当前推广期+30期</span>
            </dd>
        </dl>
        <?php
            $i = 1;
            foreach((array)$schedule_info as $k=>$v){
        ?>
        <dl id="clone_content">
            <dt>推广时间<?php echo $i ?>：</dt>
            <dd style="width: 400px" class="in_table">
                <input type="hidden" name="id<?php echo $i; ?>" value="<?php echo $v['ad_time_id']; ?>">
                <input type="text" value="<?php echo $v['start_time']; ?>" datefmt="yyyy-MM-dd HH:mm:ss" readonly="true" name="start_time<?php echo $i; ?>" class="date textInput readonly">
				<input style="margin-left: 15px" type="text" value="<?php echo $v['end_time']; ?>" datefmt="yyyy-MM-dd HH:mm:ss" readonly="true" name="end_time<?php echo $i; ?>" class="date textInput readonly focus">
                <span class="time_add" data-id ="<?php echo $i; ?>">提交</span><span class="time_del" data-id ="<?php echo $i; ?>">删除</span>
            </dd>
        </dl>
        <?php
                $i++;
            }
        ?>

        <?php
            for($j=$i;$j<30+$i;$j++){
        ?>
        <dl id="clone_content" style="display: none">
            <dt>推广时间<?php echo $j ?>：</dt>
            <dd style="width: 400px" class="">
                <input type="hidden" name="id<?php echo $j; ?>" value="">
                <input type="text" value="" datefmt="yyyy-MM-dd HH:mm:ss" readonly="true" name="start_time<?php echo $j; ?>" class="date textInput readonly">
                <input style="margin-left: 15px" type="text" value="" datefmt="yyyy-MM-dd HH:mm:ss" readonly="true" name="end_time<?php echo $j; ?>" class="date textInput readonly focus">
                <span class="time_add" data-id ="<?php echo $j; ?>">提交</span><span class="time_del" data-id ="<?php echo $j; ?>">删除</span>
            </dd>
        </dl>
        <?php } ?>
	</div>
	<div class="formBar">
		<ul>
            <li><div class="buttonActive"><div class="buttonContent"><button class="_add_schedule" type="button">新增排期</button></div></div></li>
			<li><div class="button"><div class="buttonContent"><button class="close" type="button">关闭</button></div></div></li>
		</ul>
	</div>
</div>
</form>
<script type="text/javascript">
    $(function() {
        //新增加排期时间
        var times_count = <?php echo $i; ?>;
        var over_count = parseInt(times_count)+30;
        $('._add_schedule').click(function(){
            //$.pdialog.reload("<?php echo SITE_URL . 'index.php?c='.$controller.'&a=schedule';?>&ad_id=<?php echo $info['ad_id'];?>",'',"schedule_ad");

            if(over_count == times_count){
                alertMsg.info('只允许添加未来30期的排期');
                return false;
            }
            $('.pageFormContent').find('dl').eq(times_count).show();

            times_count++;
        });

        //添加 删除 排期
        var ad_id = <?php echo $info['ad_id']; ?>;
        $('.time_add').click(function(){

            var _self = $(this);
            var num = _self.attr('data-id');

            var ad_time_id = _self.parents('dd').find('input[name=id'+num+']').val();
            var start_time = _self.parents('dd').find('input[name=start_time'+num+']').val();
            var end_time = _self.parents('dd').find('input[name=end_time'+num+']').val();

            if(ad_time_id){
                $.post("<?php echo SITE_URL . 'index.php?c='.$controller;?>&a=ajaxEditSchedule",{ad_time_id:ad_time_id,start_time:start_time,end_time:end_time},function(data){
                    if(data.retCode == 200){
                        alertMsg.correct('修改排期成功！');
                        return false;
                    }else{
                        alertMsg.error(data.message);
                        return false;
                    }
                },"json");
            }else{
                $.post("<?php echo SITE_URL . 'index.php?c='.$controller;?>&a=ajaxAddSchedule",{ad_id:ad_id,start_time:start_time,end_time:end_time},function(data){
                    if(data.retCode == 200){
                        alertMsg.correct('添加排期成功！');
                        _self.parents('dd').find('input[name=id'+num+']').val(data.id);
                        return false;
                    }else{
                        alertMsg.error(data.message);
                        return false;
                    }
                },"json");
            }
        });

        $('.time_del').die().live('click',function(){
            var _self = $(this);
            var num = _self.attr('data-id');

            var ad_time_id = _self.parents('dd').find('input[name=id'+num+']').val();

            if(ad_time_id){
                $.post("<?php echo SITE_URL . 'index.php?c='.$controller;?>&a=ajaxDelSchedule",{ad_time_id:ad_time_id},function(data){
                    if(data.retCode == 200){
                        alertMsg.correct('删除排期成功！')
                        _self.parents('dd').find('input[name=id'+num+']').val('');
                        _self.parents('dd').find('input[name=start_time'+num+']').val('');
                        _self.parents('dd').find('input[name=end_time'+num+']').val('');
                        return false;
                    }else{
                        alertMsg.error('删除排期失败，请刷新后重试!');
                        return false;
                    }
                },"json");
            }else{
                _self.parents('dd').find('input[name=id'+num+']').val('');
                _self.parents('dd').find('input[name=start_time'+num+']').val('');
                _self.parents('dd').find('input[name=end_time'+num+']').val('');
                alertMsg.info('当前排期未使用，请重新添加！');
                return false;
            }
        });
    });
</script>